package com.algrithom.find;

/**
 * 153. 寻找旋转排序数组中的最小值
 */
public class Solution6 {

    public static void main(String[] args){
        int[] array = new int[]{7,8,9,1,2,3,4,5,6};
        Solution6 solution6 = new Solution6();
        System.out.println(solution6.findMin(array));
    }
    public int findMin(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int minVal = nums[(left + right) / 2];
        if (nums[left] < nums[right]) {
            return nums[left];
        }
        while (left <= right) {
            int mid = (left + right) / 2;
            minVal = Math.min(nums[mid], minVal);
            if (nums[mid] <= minVal && minVal <= nums[nums.length - 1]) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return minVal;
    }
}
